<?php
foreach($events as $no => $event){
    $eventUrl = $this->Html->url(array('controller'=>'events','action'=>'view','slug'=>$event['Event']['slug']),true);
    $chapterImage = null;
    if(!empty($event['Chapter']['image'])){
        $chapterImage = $event['Chapter']['image'];
    } 
    $chapterUrl = "/";
    if(empty($event['Chapter']['url'])){
        $chapterUrl = $this->Html->url(array('controller'=>'chapters','action'=>'view','slug'=>$event['Chapter']['slug']),true);
    } else {
        $chapterUrl = $event['Chapter']['url'];
    }
    $event['Chapter']['url'] = $chapterUrl;
    $event['Chapter']['image'] = $this->Html->url('/static/gdglogo/'.$chapterImage, true);
    $event['Event']['startdate'] = $this->Timepl->niceShort($event['Event']['startdate']);
    $event['Event']['url'] = $eventUrl;
    unset($event['Chapter']['slug']);
    unset($event['Event']['chapter_id']);
    unset($event['Event']['slug']);
    $events[$no] = $event;
}
$result = array('data'=>$events,'next'=>$hasMore);
echo json_encode($result); 
?>